<?php


namespace App\Http\Controllers;
use App\Models\Product;
use App\Models\Category;
use App\Service\ApiResponse;
use http\Env\Request;
use Illuminate\Support\Facades\DB;

class ProductsController
{
    public function list()
    {
        $categoryModel = new Category();
        $cateList = $categoryModel::select()
            ->where('parent_id', '>', 0)
            ->orderBy('parent_id', 'asc')
            ->orderBy('sort', 'asc')
            ->get();
        foreach ($cateList as $key => $val) {
            $category[$key] = [
                'tab_id' => $val['parent_id'],
                'id' => $val['id'],
                'name' => $val['name']
            ];
        }
        $result['category'] = $category;
        $productList = DB::table('products as p')
            ->join('categorys as ca', 'ca.id', '=', 'p.category_id')
            ->where([
                'p.state' => 1,
                'p.is_delete' => 0,
            ])
            ->where('p.issue_time', '<=', date('Y-m-d H:i:s', time() + 28800))
            ->orderBy('p.orid', 'asc')
            ->orderBy('p.id', 'desc')
//            ->select(['ca.id as category_id','ca.parent_id as tab_id','ca.name as category_name', 'p.id as id','p.name as name','p.cover as image','p.name_remark as name_remark','p.miaoshu as keyword']);
            ->get(['ca.id as category_id', 'ca.parent_id as tab_id', 'ca.name as category_name', 'p.id as id', 'p.name as name', 'p.cover as image', 'p.name_remark as name_remark', 'p.miaoshu as keyword'])
            ->map(function ($item) {
                return (array)$item; // 将每个对象转换为数组
            })
            ->toArray();

        $categoryProduct1 = [];
        $categoryProduct2 = [];
        $model = new Product();
        foreach ($productList as $key => $val) {
            if ($val['tab_id'] == 1) {
                if (empty($categoryProduct1[$val['category_id']])) {
                    $categoryProduct1[$val['category_id']] = [
                        'category_id' => $val['category_id'],
                        'category_name' => $val['category_name'],
                        'products' => [[
                            'id' => $val['id'],
                            'name' => $val['name'].$val['name_remark'],
                            'image' =>$model->getContentCoverAttribute($val['image']),
                            'keyword' => $val['keyword']
                        ]]
                    ];
                } else {
                    $categoryProduct1[$val['category_id']]['products'] = array_merge($categoryProduct1[$val['category_id']]['products'], [[
                        'id' => $val['id'],
                        'name' => $val['name'].$val['name_remark'],
                        'image' => $model->getContentCoverAttribute($val['image']),
                        'keyword' => $val['keyword']
                    ]]);
                }
            } else {
                if (empty($categoryProduct2[$val['category_id']])) {
                    $categoryProduct2[$val['category_id']] = [
                        'category_id' => $val['category_id'],
                        'category_name' => $val['category_name'],
                        'products' => [[
                            'id' => $val['id'],
                            'name' => $val['name'].$val['name_remark'],
                            'image' => $model->getContentCoverAttribute($val['image']),
                            'keyword' => $val['keyword']
                        ]]
                    ];
                } else {
                    $categoryProduct2[$val['category_id']]['products'] = array_merge($categoryProduct2[$val['category_id']]['products'], [[
                        'id' => $val['id'],
                        'name' => $val['name'].$val['name_remark'],
                        'image' => $model->getContentCoverAttribute($val['image']),
                        'keyword' => $val['keyword']
                    ]]);
                }
            }
        }
        $product = [
            [
                'tab_id' => 1,
                'tab_name' => '家庭用',
                'list' => array_values($categoryProduct1)
            ],
            [
                'tab_id' => 2,
                'tab_name' => '业务用',
                'list' => array_values($categoryProduct2)
            ]
        ];

        $result['list'] = $product;
        return ApiResponse::success($result);
    }

    public function detail()
    {
        $params = $_POST;
        $id = $params['id'];
        $productInfo = DB::table('products as p')
            ->join('categorys as ca', 'ca.id', '=', 'p.category_id')
            ->where('p.id', '=', $id)
            ->first([
                'ca.id as category_id', 'ca.parent_id as tab_id', 'ca.name as category_name', 'p.id as id', 'p.name as name', 'p.cover as image', 'p.name_remark as name_remark', 'p.miaoshu as keyword',
                'p.linkurl as brand_linkurl', 'p.brand_id as brand_id', 'p.description as description', 'p.cover as cover_image', 'p.video_cover as video_image',
                'p.video_url as video', 'p.content_cover as detail_image', 'p.guomin as allergy_info', 'p.content as product_info', 'p.sku as sku'
            ]);

        $model = new Product();
        $productInfo->cover_image = empty($productInfo->detail_image) ? '' : $model->getContentCoverAttribute($productInfo->detail_image);
        $productInfo->video_image = empty($productInfo->video_image) ? '' : $model->getContentCoverAttribute($productInfo->video_image);
        $productInfo->video = empty($productInfo->video) ? '' : $model->getContentCoverAttribute($productInfo->video);
        $productInfo->detail_image = empty($productInfo->detail_image) ? '' : $model->getContentCoverAttribute($productInfo->detail_image);
        if (!empty($productInfo->sku)) {
            $sku = json_decode(json_decode($productInfo->sku),true);
            $formattedProducts = [];
            foreach ($sku as $v) {
                $formattedProducts[] = [
                    'taste' => $v['口味'],
                    'net_content' => $v['产品规格'],
                    'expiration' => $v['保质期']
                ];
            }
            $productInfo->sku = $formattedProducts;
        }
        return ApiResponse::success($productInfo);
    }
}
